<template>
  <div class="achievements-container gradient-bg">
    <header class="header glass">
      <el-button @click="$router.back()" text>
        <el-icon><ArrowLeft /></el-icon>
        返回
      </el-button>
      <h2>成就墙</h2>
      <div class="stats">
        <span>已解锁: {{ unlockedCount }}/{{ totalCount }}</span>
      </div>
    </header>

    <div class="achievements-grid">
      <div 
        v-for="achievement in achievements"
        :key="achievement.id"
        class="achievement-card glass"
        :class="{ unlocked: achievement.unlocked }"
      >
        <div class="icon">{{ achievement.icon }}</div>
        <h3>{{ achievement.name }}</h3>
        <p>{{ achievement.description }}</p>
        <div class="progress" v-if="!achievement.unlocked">
          <el-progress 
            :percentage="achievement.progress" 
            :show-text="false"
          />
          <span>{{ achievement.current }}/{{ achievement.target }}</span>
        </div>
        <div class="unlocked-badge" v-else>
          <el-icon><Check /></el-icon>
          已解锁
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { ArrowLeft, Check } from '@element-plus/icons-vue'
import { achievementApi } from '@/api/achievement'

const achievements = ref<any[]>([])

const unlockedCount = computed(() => 
  achievements.value.filter(a => a.unlocked).length
)

const totalCount = computed(() => achievements.value.length)

// 图标映射表
const iconMap: Record<string, string> = {
  // 数据库中的实际值
  book: '📚',
  medal: '🏅',
  trophy: '🏆',
  task: '✅',
  friend: '👥',
  star: '⭐',
  master: '🎖️',
  crown: '👑',
  streak: '🔥',
  game: '🎮',
  perfect: '💯',
  pet: '🐾',
  // 其他可能的值
  beginner: '🎯',
  study: '📚', 
  learn: '📖',
  // 默认图标
  default: '🎯'
}

const loadAchievements = async () => {
  try {
    const res = await achievementApi.getUserAchievements()
    if (res.success && res.data) {
      achievements.value = res.data.map((a: any) => ({
        ...a,
        icon: iconMap[a.icon] || a.icon || '🎯',
        progress: a.target > 0 ? Math.min((a.current / a.target) * 100, 100) : 0
      }))
    }
  } catch (error) {
    // 如果API未实现，使用模拟数据
    achievements.value = [
      {
        id: 1,
        icon: '🎯',
        name: '初学者',
        description: '学习10个单词',
        current: 5,
        target: 10,
        unlocked: false
      },
      {
        id: 2,
        icon: '📚',
        name: '勤奋好学',
        description: '连续学习7天',
        current: 3,
        target: 7,
        unlocked: false
      },
      {
        id: 3,
        icon: '🏆',
        name: '满分达人',
        description: '获得10次满分',
        current: 2,
        target: 10,
        unlocked: false
      },
      {
        id: 4,
        icon: '⭐',
        name: '星星收集者',
        description: '累计获得100颗星星',
        current: 45,
        target: 100,
        unlocked: false
      },
      {
        id: 5,
        icon: '🎮',
        name: '游戏高手',
        description: '完成10局游戏',
        current: 0,
        target: 10,
        unlocked: false
      },
      {
        id: 6,
        icon: '🐾',
        name: '宠物达人',
        description: '宠物等级达到10级',
        current: 1,
        target: 10,
        unlocked: false
      }
    ].map(a => ({
      ...a,
      progress: (a.current / a.target) * 100
    }))
  }
}

onMounted(() => {
  loadAchievements()
})
</script>

<style scoped lang="scss">
.achievements-container {
  width: 100%;
  min-height: 100vh;
  padding: 20px;
  overflow-y: auto;
  padding-bottom: 60px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 30px;
  margin-bottom: 30px;
  
  h2 {
    color: white;
    margin: 0;
    font-size: 1.5rem;
  }
  
  .stats {
    color: white;
    font-size: 1.1rem;
    font-weight: bold;
  }
}

.achievements-grid {
  max-width: 1200px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
  padding: 20px;
}

.achievement-card {
  padding: 30px 25px;
  text-align: center;
  transition: all 0.3s;
  position: relative;
  
  &:not(.unlocked) {
    opacity: 0.6;
  }
  
  &.unlocked {
    box-shadow: 0 8px 32px 0 rgba(255, 215, 0, 0.5);
    animation: glow 2s ease-in-out infinite alternate;
  }
  
  &:hover {
    transform: translateY(-5px);
  }
  
  .icon {
    font-size: 4rem;
    margin-bottom: 15px;
  }
  
  h3 {
    color: #FFD700;
    font-size: 1.3rem;
    font-weight: 600;
    margin-bottom: 10px;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
  }
  
  p {
    color: rgba(255, 255, 255, 0.95);
    font-size: 1rem;
    margin-bottom: 20px;
    min-height: 40px;
    line-height: 1.5;
    font-weight: 500;
  }
  
  .progress {
    span {
      display: block;
      margin-top: 10px;
      color: rgba(255, 255, 255, 0.9);
      font-size: 0.9rem;
    }
  }
  
  .unlocked-badge {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 10px 20px;
    background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
    border-radius: 20px;
    color: white;
    font-weight: bold;
    margin: 0 auto;
    width: fit-content;
  }
}

@keyframes glow {
  from {
    box-shadow: 0 8px 32px 0 rgba(255, 215, 0, 0.3);
  }
  to {
    box-shadow: 0 8px 32px 0 rgba(255, 215, 0, 0.7);
  }
}
</style>
